package com.ct.book.controller;


import com.ct.book.constants.Constants;
import com.ct.book.model.UserInfo;
import com.ct.book.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Boolean login (String name, String password, HttpSession session) {
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(password)) {
            return false;
        }
        UserInfo userInfo = userService.queryUsrByName(name);
        if (userInfo == null) {
            return false;
        }
        if (password.equals(userInfo.getPassword())) {
            session.setAttribute(Constants.SESSION_USER_KEY, userInfo);
            return true;
        }
        return false;
    }
}
